module.exports = {
	log_upload_save(event) {
		let _this = this;
		let { item, value, step = 0, callback } = event;

		if (step == 0) {
			let { click_type, view_tt, value_type, select_list, unit } = item;
			let config = {
				switch: {
					0: '关闭',
					1: '开启',
				},
				balanced: {
					0: '静态均衡',
					1: '充电均衡',
				},
			};
			uni.removeStorageSync('operation_msg');
			// 保存用户操作到缓存
			let operation_msg = '';

			switch (click_type) {
				case 'switch':
					operation_msg = `${view_tt}：${config['switch'][value]}`;
					break;
				case 'select':
					if (item.form_key == 'function_setting_balance_mode') {
						operation_msg = `${view_tt}：${config['balanced'][value]}`;
					} else {
						operation_msg = `${view_tt}：${select_list[value]['title']}`;
					}
					break;
				case 'confirm':
					operation_msg = `控制页面：${view_tt}`;
					if (value >= 0) {
						operation_msg += `_${value}`;
						if (_this.objJudge(unit)) {
							operation_msg += unit;
						}
					}
					break;
				case 'mode_select':
					operation_msg = `充电模式：${view_tt}`;
					break;
				default:
					operation_msg = `${view_tt}：${value}`;
					if (_this.objJudge(unit)) {
						operation_msg += unit;
					}
					break;
			}
			uni.setStorageSync('operation_msg', operation_msg);
		} else if (step == 1) {
			let { user_phone } = _this.USER_MSG;
			let operation_msg = uni.getStorageSync('operation_msg');
			let dev_id = _this.BLUETOOTH_DEVICE_CONFIG.bluetooth_device_mac;

			if (!operation_msg || !dev_id || !user_phone) return;
			// 经过websocket发送的修改指令不需要上传到后台操作记录
			// console.log({
			//     operation_msg,
			//     dev_id,
			//     user_phone
			// })
			// 上传操作日志
			_this.com_getdata({
				url: '/bms/operation',
				data: {
					dev_id,
					user_phone,
					operation_msg,
				},
				data_fun: function (v) {
					if (v.state == 1) {
						uni.removeStorageSync('operation_msg');
						callback && callback();
					}
				},
			});
		}
	},

	binding_dev(deviceId, mac) {
		// 进行设备绑定
		let _this = this;
		_this.com_getdata({
			url: '/user/bind',
			id_name: 'user_id',
			data: {
				dev_id: mac,
			},
			log: false,
			data_fun: function (v) {
				if (v.state == 1) {
					// 首次绑定成功连接后弹窗显示广告位

					uni.setStorageSync('FRIST_BINDING', true);
					_this.create_connect(deviceId);
				} else {
					_this.com_msg(v.msg || '网络请求错误');
				}
			},
		});
	},

	async create_connect_judge(NEXT_DEVICEID, NEXT_MAC, DEVICE) {
		// 判断是否可以连接
		let _this = this;
		if (!_this.com_loading({ status: 1, type: 'none', throttle: true })) return;

		let connect_status = await _this.check_dev(NEXT_MAC);
		let { user_status = 0 } = _this.USER_MSG;
		let { bluetooth_connect, bluetooth_deviceId } = _this.BLUETOOTH_DEVICE_CONFIG;
		if (connect_status == -2) {
			_this.com_loading({ status: 0, throttle: true });
			// 后台没有这个设备数据
			_this.com_modal({
				title: '提示',
				content: `当前设备【${NEXT_MAC}】未录入系统`,
				confirmText: '我知道了',
				showCancel: false,
			});
			return;
		} else {
			if (user_status == 1 || user_status == 2) {
				connect_status = 1;
			}
			if (connect_status == -1) {
				_this.com_loading({ status: 0, throttle: true });
				// 已绑定
				_this.com_modal({
					title: '提示',
					content: '当前设备已被绑定',
					confirmText: '我知道了',
					showCancel: false,
				});
				return;
			} else if (connect_status == 0) {
				_this.com_loading({ status: 0, throttle: true });
				// 未绑定
				let f = await _this.com_modal({
					title: '提示',
					content: '当前设备可绑定，是否去绑定？',
				});
				if (!f) return;
				_this.binding_dev(NEXT_DEVICEID, NEXT_MAC);
				return;
			} else if (connect_status == 1) {
				// 可连接
			}
		}
		// 当前已连接的话需要断开当前设备再连接下一个设备
		if (bluetooth_deviceId && bluetooth_connect) {
			_this.close_connect(bluetooth_deviceId, () => {
				setTimeout(() => {
					_this.create_connect(NEXT_DEVICEID, DEVICE);
				}, 300);
			});
			return;
		}
		if (NEXT_DEVICEID) {
			_this.create_connect(NEXT_DEVICEID, DEVICE);
		}
	},
};
